import Vue from 'vue'
import App from './App.vue'
import router from './router'
import api from './api/index'
import VueRouter from 'vue-router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import './assets/css/common.css'
import './utils/override'

Vue.use(VueRouter)
Vue.use(ElementUI);
Vue.prototype.$api = api;
Vue.config.productionTip = false
// 捕获push replace中的错误
// 当然在replace中的错误也是可以相同的进行捕获
const originalPush = VueRouter.prototype.push
const originalReplace = VueRouter.prototype.replace
// push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
	if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
	return originalPush.call(this, location).catch(err => err)
}
// replace
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
	if (onResolve || onReject) return originalReplace.call(this, location, onResolve, onReject)
	return originalReplace.call(this, location).catch(err => err)
}

router.beforeEach((to, from, next) => {
	if (to.meta.title) {
		document.title = to.meta.title
	}
	if (localStorage.getItem('token')) { //判断本地是否存在access_token
		next();
	} else {
		if (to.path === '/login') {
			next();
		} else {
			setTimeout(function () {
				next({
					path: '/login'
				})
			}, 500)
		}
	}
});

new Vue({
	render: h => h(App),
	router
}).$mount('#app')
